package cn.com.ghy.android.service;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.com.ghy.android.bean.LoginInfoBean;
import cn.com.ghy.android.bean.VisitPlanBean;
import cn.com.ghy.android.bean.VisitPlanBeanParser;
import cn.com.ghy.android.bean.VisitPlanWorkTypeBean;
import cn.com.ghy.android.bean.VisitPlanWorkTypeBeanParser;
import cn.com.ghy.android.util.CommonStatic;
import cn.com.ghy.android.util.CommonUtil;

/**
 * @软件著作权:金红叶纸业集团有限公司
 * @系统名称:SFA系统
 * @Description:下月拜访计划服务器
 * @author wuz  @date 2011-5-24 14:51:45
 * @版本历史:v1.0
 * @Copyright:Copyright (c) 2011-2012 
 */

public class PlanOpeService {
	private String url = CommonStatic.URL_HEAD + "PlanOpe_Ar.asmx";
	private String methodName = "";
	private Map<String, String> params = null;

	private static final String USERID = "userid";
	private static final String MONTHID = "monthid";
	private static final String PLANDETAIL = "plandetail";

	public String GetPlanState(LoginInfoBean loginInfo, String monthid) throws Exception {
		methodName = "GetPlanState";

		params = new HashMap<String, String>();
		params.put(USERID, loginInfo.getPernr());
		params.put(MONTHID, monthid);
		String result = HttpService.callWS(url, methodName, params);
		return result;
	}

	public List<VisitPlanWorkTypeBean> WorkTypeList() throws Exception {
		methodName = "WorkTypeList";
		params = new HashMap<String, String>();

		String result = HttpService.callWS(url, methodName, params);
		byte[] buffer = org.kobjects.base64.Base64.decode(result);
		byte[] info = CommonUtil.decompressByteArray(buffer);
		InputStream in = new ByteArrayInputStream(info);
		List<VisitPlanWorkTypeBean> list = VisitPlanWorkTypeBeanParser.parseXML(in);
		in.close();
		CommonUtil.checkListNull(list);
		return list;
	}

	public List<VisitPlanBean> GetPlan(LoginInfoBean loginInfo, String monthid) throws Exception {
		methodName = "GetPlan";

		params = new HashMap<String, String>();
		params.put(USERID, loginInfo.getPernr());
		params.put(MONTHID, monthid);

		String result = HttpService.callWS(url, methodName, params);
		byte[] buffer = org.kobjects.base64.Base64.decode(result);
		byte[] info = CommonUtil.decompressByteArray(buffer);
		InputStream in = new ByteArrayInputStream(info);
		List<VisitPlanBean> list = VisitPlanBeanParser.parseXML(in);
		in.close();
		CommonUtil.checkListNull(list);
		return list;
	}

	public String SetPlan(LoginInfoBean loginInfo, String monthid, List<VisitPlanBean> list)
			throws Exception {
		methodName = "SetPlan";
		String plandetailXML = "";

		StringWriter writer = new StringWriter();
		plandetailXML = VisitPlanBeanParser.createXMLPlandetail(list, monthid, writer);
		writer.close();
		plandetailXML = org.kobjects.base64.Base64.encode(CommonUtil
				.compressByteArray(plandetailXML.getBytes()));

		params = new HashMap<String, String>();
		params.put(USERID, loginInfo.getPernr());
		params.put(MONTHID, monthid);
		params.put(PLANDETAIL, plandetailXML);

		String result = HttpService.callWS(url, methodName, params);
		return result;
	}

	public String SendPlan(LoginInfoBean loginInfo, String monthid) throws Exception {
		methodName = "SendPlan";

		params = new HashMap<String, String>();
		params.put(USERID, loginInfo.getPernr());
		params.put(MONTHID, monthid);

		String result = HttpService.callWS(url, methodName, params);
		if (null != result) {
			return result;
		}
		return null;
	}
}
